# -*- encoding: utf-8 -*-

# Copyright 2010 István Szentandrási
# <etude-music-player@googlegroups.com>

# This file is part of Etude.
#
# Etude is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as 
# published by the Free Software Foundation, either version 3 
# of the License, or (at your option) any later version.
#
# Etude is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with Etude. If not, see <http://www.gnu.org/licenses/>.

import gobject
import xmmsclient
import xmmsclient.propdict

from common import *
from etude import config

class MediaInfo(xmmsclient.propdict.PropDict):

    """
    Enhanced propdict for storing information about a medialib id
    """

    def __init__(self, info = None):
        if not isinstance(info, xmmsclient.propdict.PropDict):
            info = xmmsclient.propdict.PropDict(['server', 'plugin/*', 'client/*', '*'])
        xmmsclient.propdict.PropDict.__init__(self, info.sources)
        self.set_info(info)

    def set_info(self, info):
        self.clear()
        self.update(info)

    def get_property_source(self, property):
        for src in self.sources:
            if src.endswith('*'):
                for k,v in self.iteritems():
                    if k[0].startswith(src[:-1]) and k[1] == property:
                        return k[0]
            try:
                t = dict.__getitem__(self, (src, property))
                return src
            except KeyError:
                pass
        raise KeyError, property

    def __setitem__(self, key, value):
        try:
            src = self.get_property_source(key)
        except KeyError:
            src = ''
        dict.__setitem__(self, (src, key), value)


    def get_custom_info(self, format):
        """
        to group propertios use {} - the contents are only shown if all the
        properties inside are set

        | is a separator, function returns a list
        <artist> - artist
        <album> - album
        <smart> - title or filename or channelinstead if not found
        <title> - title
        <channel> - radio channel
        <tracknr> - tracknumber
        <url> - path - if file
        <bitrate>- bitrate
        <filename>- filename - if file
        <duration> - song length
        <date> - date
        <genre> - genre
        <comment> - comment

        <id> - medialib id
        <laststarted> - Unix last started
        <added> - added to medialib
        <timesplayed> - times played
        """
        """glob = [value for key, value in globals().items() if key.startswith("XMEP_")]
        dict = {}
        for value in glob:
            if value in ('id', 'bitrate', 'tracknr', 'timesplayed','laststarted', 'added',
                    'duration'):
                min = 0
            else:
                min = ''
            dict[value] = max(self.get(value), min)
        """
        return config.parse_row(format, self)

